

/**
 * uni.showToast提示窗
 * @param {String | Object} options 提示内容title或弹窗参数
 * @param {String} options.title 提示内容
 * @param {String} options.icon 弹窗图标
 * @param {Number} options.duration 弹窗时长，单位ms
 * @param {Boolean} options.mask 弹窗遮罩，默认为true
 * @param {Function} options.callback 弹窗结束时回调函数
 */
function useToast(options = {}) {
  if (typeof options === "string") {
    const title = options;
    options = { title };
  }

  options = {
    title: "",
    icon: "none",
    duration: 2000,
    mask: true,
    callback: undefined,
    ...options,
  };

  return new Promise((resolve) => {
    uni.showToast(options).then(() => {
      const { callback, duration } = options;

      if (typeof callback !== "function") {
        return resolve();
      }

      setTimeout(() => {
        callback();
        resolve();
      }, duration);
    });
  });
}

export default useToast;
